package org.yue.netty.servlet;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.ssl.SslContext;

import javax.servlet.ServletContext;

/**
 * Created by root on 4/13/17.
 */
public class ServletServerInitializer extends ChannelInitializer<SocketChannel> {

    private final SslContext sslCtx;
    private final ServletContext servletContext;

    public ServletServerInitializer(SslContext sslContext, ServletContext servletContext) {
        this.sslCtx = sslContext;
        this.servletContext = servletContext;
    }

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline p = ch.pipeline();
        if (sslCtx != null) {
            p.addLast(sslCtx.newHandler(ch.alloc()));
        }
        p.addLast(new HttpServerCodec());
        p.addLast(new ServletServerHandler(servletContext));

    }
}
